{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Please note that you need to have the OSGeo (Gdal) Python package installed.\n",
    "\n",
    "```bash\n",
    "\n",
    " conda install gdal\n",
    " OR\n",
    " pip install pygdal==\"`gdal-config --version`.*\"\n",
    " python\n",
    " >>>from osgeo import gdal\n",
    "\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyfesom2 import load_mesh,select_slices,fesom2GeoFormat\n",
    "import xarray as xr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set Path to the mesh and result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "meshPath  ='../tests/data/pi-grid/' \n",
    "result_path ='../tests/data/pi-results/temp.fesom.1948.nc'\n",
    "alpha, beta, gamma=[50, 15, -90]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load the mesh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Load 2d part of the mesh in 0 second(s)\n"
     ]
    }
   ],
   "source": [
    "mesh=load_mesh(meshPath,abg=[alpha, beta, gamma], usepickle = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### load and slicing the data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "result_Obj = xr.open_mfdataset(result_path,combine=\"by_coords\")\n",
    "data=select_slices(result_Obj,\"temp\",mesh,[0],500)\n",
    "data.mean(dim=\"time\")\n",
    "data=data.compute().data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### calling fesom2GeoFormat\n",
    "+ Data = **numpy array**\n",
    "+ mesh = **pyfesom mesh representation**\n",
    "+ outName = **Output Name with extension \"exampl.tif\" or \"example.nc\"**\n",
    "+ radius_of_influence = **Cut off distance in meters, only used in nn and idist.Default=100000**\n",
    "+ driver = **String \"Gtiff\" for geoTiff and \"netCDF\" for netcdf**\n",
    "+ method=  **Interpolation method. Options are 'nn' (nearest neighbor), 'idist' (inverce distance), \"linear\" and \"cubic\"**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "fesom2GeoFormat(\n",
    "    data,\n",
    "    mesh,\n",
    "    'newTest.tif',\n",
    "    method='nn',\n",
    "    driver='Gtiff',\n",
    "    radius_of_influence=500000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Driver: GTiff/GeoTIFF\r\n",
      "Files: newTest.tif\r\n",
      "Size is 360, 168\r\n",
      "Coordinate System is:\r\n",
      "GEOGCS[\"WGS 84\",\r\n",
      "    DATUM[\"WGS_1984\",\r\n",
      "        SPHEROID[\"WGS 84\",6378137,298.257223563,\r\n",
      "            AUTHORITY[\"EPSG\",\"7030\"]],\r\n",
      "        AUTHORITY[\"EPSG\",\"6326\"]],\r\n",
      "    PRIMEM[\"Greenwich\",0],\r\n",
      "    UNIT[\"degree\",0.0174532925199433],\r\n",
      "    AUTHORITY[\"EPSG\",\"4326\"]]\r\n",
      "Origin = (-179.963005891225805,89.404839573043475)\r\n",
      "Pixel Size = (1.000000000000000,-1.000000000000000)\r\n",
      "Metadata:\r\n",
      "  AREA_OR_POINT=Area\r\n",
      "Image Structure Metadata:\r\n",
      "  INTERLEAVE=BAND\r\n",
      "Corner Coordinates:\r\n",
      "Upper Left  (-179.9630059,  89.4048396) (179d57'46.82\"W, 89d24'17.42\"N)\r\n",
      "Lower Left  (-179.9630059, -78.5951604) (179d57'46.82\"W, 78d35'42.58\"S)\r\n",
      "Upper Right ( 180.0369941,  89.4048396) (180d 2'13.18\"E, 89d24'17.42\"N)\r\n",
      "Lower Right ( 180.0369941, -78.5951604) (180d 2'13.18\"E, 78d35'42.58\"S)\r\n",
      "Center      (   0.0369941,   5.4048396) (  0d 2'13.18\"E,  5d24'17.42\"N)\r\n",
      "Band 1 Block=360x22 Type=Byte, ColorInterp=Gray\r\n"
     ]
    }
   ],
   "source": [
    "!gdalinfo newTest.tif\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fesom",
   "language": "python",
   "name": "fesom"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}